package reader;
import java.net.*;
import java.io.*;

import edu.boun.cmpe.elele.cotrans.textOperations.Html2Text;

public class URLConnectionReader {
        
    public String parseWiki(String url){
    	String result="";
    	
    	URL yahoo;
		try {
			yahoo = new URL(url);
			URLConnection yc = yahoo.openConnection();
	        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
	        
	        String res="";
	        String inputLine;

	        while ((inputLine = in.readLine()) != null) {
	            //System.out.println(inputLine);
	            res+=inputLine;
	        }
	        
	        String temp[]=res.split("<!-- start content -->");
	        String temp2[]= temp[1].split("<!-- end content -->");
	        String s=temp2[0].replace("href=\"/wiki", "href=\"http://en.wikipedia.org/wiki").replace("href=\"/w/", "href=\"http://en.wikipedia.org/w/");
	        String[] resTemp= s.split("title=\"Edit section: References\"");
	        resTemp= resTemp[0].split("title=\"Edit section: Notes\"");
	        String son= resTemp[0];
	        son=son.replace("[<a", "<a").replace("</a>]", "</a>").replace(">edit<","><");
	        System.out.println(son);
	        result=son;
	        in.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    	Html2Text deneme= new Html2Text();
    	System.out.println(deneme.getContent(result));
    	
    	return result;
    }
}
